package com.olmarket.core.service;

import com.olmarket.core.entity.PageResource;
import com.olmarket.core.entity.User;
import com.olmarket.core.entity.constant.UserType;
import com.olmarket.core.exception.Asserts;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author h'zh
 * @date: 12.14
 */

//TODO:修改访问权限
@Service
public class PermissionService {
    // 权限相关
    // url资源的访问控制
    // 根据user 获取 user能够访问的url列表
    // 前端添加的页面，需要在此处指定
    public List<PageResource> getAccessUrlsOfUser(User user) {
        if (null == user) {
            Asserts.fail("user not found");
        }
        int userType = user.getUserType();
        List<PageResource> ret = new ArrayList<>();
        switch (userType) {
            case UserType.ADMIN: {
                String[] urls = new String[]{
                        "/admin/studentList", "/admin/goodList",
                        "/admin", "/admin/requestList",
                        "/admin/typeList",

                };
                return Stream.of(urls).map(PageResource::new).collect(Collectors.toList());
            }
            case UserType.STUDENT: {
                String[] urls = new String[]{
                        "/student", "/student/Goods", "/group/upload", "/group/result", "/student/Requests",
                        "/student/Management/personalInfo",
                        "/student/Management/goodManage",
                        "/student/Management/requestManage",
                        "/student/Management/accountManage",
                        "/student/Management"
                };
                return Stream.of(urls).map(PageResource::makeInstance).collect(Collectors.toList());
            }
            default:
                break;
        }

        return ret;
    }

}
